var _ = require('lodash');
var config = require('../../config');
var whost = config.whost;
var request = require('../../support').request2;

/**
 * 验证登录是否成功
 * @return promise
 */
exports.login = function (context, data) {
  return request(context, {
    url: whost + '/hk-api/api/json/user/hkLogin',
    method: 'post',
    data: data
  });
};

/**
 * 获取验证码KEY
 * @return promise
 */
exports.getCaptcha = function (context) {
  return request(context, {
    url: whost + '/hk-api/api/json/brick/createVerifyKey',
  }).then(function (data) {
    var params = {
      verifyKey: data.verifyKey
    };
    return {
      key: data.verifyKey,
      captcha: config.whost + '/hk-api/api/json/brick/imgVerifyCode?body=' + encodeURIComponent(JSON.stringify(params))
    };
  });
};

/**
 * 获取用户详情
 * @return {promise}
 */
exports.getMember = function (context, memberId, accountId) {
  return request(context, {
    url: whost + '/hk-api/api/json/user/getHkRoleInfoByMidAndUid',
    data: {
      memberId: memberId,
      userId: accountId,
    }
  }).then(function (res) {
    if (res.accountEntity && res.accountEntity.accountId) {
      return res;
    }
    else {
      return Promise.reject(res);
    }
  });
};